package com.servlet;

import com.dao.ManagerService;
import com.dao.impl.ManagerServiceImpl;
import com.entity.Manager;
import com.google.gson.Gson;
import com.servlet.teacher.BaseServlet;
import com.util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author ly
 * @dATE 2022-03-25 14:26
 */
@WebServlet("/ManagerServlet")
public class ManagerServlet extends BaseServlet {
    ManagerService managerService = new ManagerServiceImpl();
    /*
    * 确认管理员
    */
    public void confirmManager(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String password = req.getParameter("password");

        Manager manager = new Manager();

        manager.setId(Integer.parseInt(id));

        Gson gson = new Gson();
        JsonUtil jsonUtil = new JsonUtil();

        try {
            List<Manager> managers = managerService.queryData(manager);
            System.out.println("password "+password);
            System.out.println("ma "+managers.get(0).getPassword());
            if(managers.size() != 0){
                if(managers.get(0).getPassword().equals(password)){
                    jsonUtil.setCode(200);
                    jsonUtil.setMsg("正确");
                }else {
                    jsonUtil.setCode(111);
                    jsonUtil.setMsg("密码错误");
                }
            }
        } catch (Exception e) {
            jsonUtil.setCode(500);
            jsonUtil.setMsg("系统内部错误");
            e.printStackTrace();
        }

        String toJson = gson.toJson(jsonUtil);
        PrintWriter writer = resp.getWriter();
        writer.print(toJson);
    }
}
